var ScuControl_debug=false ;

var ScuControl_gLayout = {};

var ScuControl_gSocketIo = io.connect();

var ScuControl_gcbOnRecvLayout ;
var ScuControl_gcbOnUpdate ;

function ScuControl_Init(onLayout, onUpdate)
{
    if( ScuControl_debug )
        local_log('ScuControl_Init');
        
	ScuControl_gSocketIo.on('layout', ScuControl_onRecvLayout) ;
	ScuControl_gSocketIo.on('nodevice', ScuControl_onNoDevice) ;
	ScuControl_gSocketIo.on('update', ScuControl_onUpdate) ;
	
	ScuControl_gcbOnRecvLayout = onLayout ;
	ScuControl_gcbOnUpdate = onUpdate ;
	
	return ScuControl_gSocketIo ;
}

/* Receive CubeLayout.xml & status Information by cubeserver.js makePacketLayout()
   send layout to client
*/
function ScuControl_onRecvLayout(str)
{
    if( ScuControl_debug )
        local_log('ScuControl_onRecvLayout '+str);
    
    if( null!=(ScuControl_gLayout=GetLayoutFromPacket(str)) ) {
        ScuControl_gcbOnRecvLayout(ScuControl_gLayout) ;
    }
}

/* 
*/
function ScuControl_onNoDevice(str)
{
    if( ScuControl_debug )
        local_log('ScuControl_onNoDevice '+str);

    // 안해도 되긴 하는데, 기존 control에서 하므로 일단은...
	//ScuControl_gSocketIo_emit('nodevice', str);
}

/* Receive $update:[0,10,0,0,],$
*/
function ScuControl_onUpdate(str)
{
    if( ScuControl_debug )
        local_log('ScuControl_onUpdate '+str);
    
    var update ;
    if( null!=(update= GetUpdateFromPacket(str)) ) {
        ScuControl_UpdateRelayStatus(update) ;
        ScuControl_gcbOnUpdate(update) ;
    }

    // 안해도 되긴 하는데, 기존 control에서 하므로 일단은...
	//ScuControl_gSocketIo_emit('update', str);
}

function ScuControl_GetRelayInfo(cid,bid,rid) 
{
    var info={} ;
    info.status = RELAY_STATUS_NOTUSED ;
    info.stime = 0 ;
    info.etime = 0 ;
    
    // find in gLayoout
    if( null!=ScuControl_gLayout && null!=ScuControl_gLayout.Relays && null!=ScuControl_gLayout.Relays ) {
		for (var i=0; i<ScuControl_gLayout.Relays.length; i++) {
			if ((ScuControl_gLayout.Relays[i].cid == cid) && (ScuControl_gLayout.Relays[i].bid == bid) && (ScuControl_gLayout.Relays[i].rid == rid)) {
			    info.status = ScuControl_gLayout.Relays[i].status ;
			    info.stime = ScuControl_gLayout.Relays[i].stime ;
			    info.etime = ScuControl_gLayout.Relays[i].etime ;
			}
		}
    }

    //local_log('ScuControl_GetRelayStatus '+cid+'/'+bid+'/'+rid+' '+RELAY_STATUS.get(status)+'('+status+')') ;
    
    return info ;
}

// update internal status
function ScuControl_UpdateRelayStatus(update) 
{
    // find in gLayoout
    if( null!=ScuControl_gLayout && null!=ScuControl_gLayout.Relays && null!=ScuControl_gLayout.Relays ) {
		for (var i=0; i<ScuControl_gLayout.Relays.length; i++) {
			if ((ScuControl_gLayout.Relays[i].cid == update.cid) && (ScuControl_gLayout.Relays[i].bid == update.bid) && (ScuControl_gLayout.Relays[i].rid == update.rid)) {
			    ScuControl_gLayout.Relays[i].status = update.status ;
			    ScuControl_gLayout.Relays[i].stime = update.stime ;
			    ScuControl_gLayout.Relays[i].etime = update.etime ;
			}
		}
    }
}

/* start of
    control each relay */
/*
var ScuControl_SetRelayList_list ;
// control multiple relay. apply delay to prevent control is skipped
function ScuControl_SetRelayList(a_relay_list,io) 
{
	local_log('ScuControl_SetRelayList '+JSON.stringify(a_relay_list)) ;
	ScuControl_SetRelayList_list = a_relay_list ;

	ScuControl_SetRelayList_list.index = 0 ;
	ScuControl_SetRelayList_list.delay = 200 ;
	ScuControl_SetRelayList_Do(io) ;
}

function ScuControl_SetRelayList_Do(io) 
{
    // end condition
    if( ScuControl_SetRelayList_list.index>=ScuControl_SetRelayList_list.count )
        return ;
        
    // control
    var relay_control = ScuControl_SetRelayList_list.list[ScuControl_SetRelayList_list.index] ;

	dual_log('ScuControl_SetRelayList_Do '+JSON.stringify(relay_control),io) ;
	ScuControl_SetRelay(relay_control.cid,relay_control.bid,relay_control.rid,relay_control.state) ;

	ScuControl_SetRelayList_list.index++ ;

    // set timer for next item
    if( ScuControl_SetRelayList_list.index<ScuControl_SetRelayList_list.count ) {
        setTimeout(function () {ScuControl_SetRelayList_Do(io)},ScuControl_SetRelayList_list.delay);
    }
}
*/
/* end of 
    control each relay */

/* start of
    control relay list */
function ScuControl_SetRelayList(a_relay_list) 
{
	var setPkt = '' ;
	
	// make command header
	setPkt += '$set:' ;

	for( i=0 ; i<a_relay_list.list.length ; i++ ) {
	    // make list
    	setPkt += '[' + a_relay_list.list[i].cid + ',' + a_relay_list.list[i].bid + ',' + a_relay_list.list[i].rid + ',' + a_relay_list.list[i].state + ',],';
	}

	// make command tail
	setPkt += '$' ;

	ScuControl_gSocketIo_emit('set', setPkt);
}
/* end of
    control relay list */

function ScuControl_SetRelay(cid,bid,rid,status) 
{
    var onoff='on' ;
    if( status==0 )
        onoff='off' ;
    if( ScuControl_debug )
        local_log('ScuControl_SetRelay id '+cid+'/'+bid+'/'+rid+' '+onoff);
    
	var setPkt = '$set:[' + cid + ',' + bid + ',' + rid + ',' + status + ',],$';
	ScuControl_gSocketIo_emit('set', setPkt);
}

function ScuControl_GetStimeString(stime,etime) 
{
    var sStr="" ;
	if(0!=stime){
		var d = new Date();
		d.setTime(stime*1000);
		sStr = d.getMonth()+1+'월'+d.getDate()+'일 '+d.getHours()+':'+d.getMinutes();
	}
	
	return sStr ;
}

function ScuControl_GetEtimeString(stime,etime) 
{
    var eStr="" ;
	if(0!=stime){
		var d = new Date();
		d.setTime(stime*1000);
		eStr = ScuControl_formatTime2(etime);
	}
	
	return eStr ;
}

function ScuControl_formatTime2(seconds) {
	var hour = Math.floor(seconds/3600);
	var min = Math.floor((seconds - hour*3600)/60);
                var sec = Math.floor(seconds - hour * 3600 - min * 60);
	
	var format;
	if (10 > hour) {
		format = '0';
		format += hour;
	}else{
		format = hour;
	}
	format += '시간';
	if (10 > min) {
		format += '0';
	}
	format += min;
	format += '분';
	//if (10 > sec) {
	//	format += '0';
	//}
	//format += sec;

	return format;
}

function ScuControl_gSocketIo_emit(arg1,arg2) {
    //if( ScuControl_debug )
        dual_log('emit :'+arg1+' / '+arg2,list_soket_io);
    ScuControl_gSocketIo.emit(arg1,arg2) ;
}

